package com.xingshentech.ugv.operate.module.httpdata;

import android.accounts.NetworkErrorException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.xingshentech.ugv.operate.module.bean2.BaseResponseCenter;
import com.xingshentech.ugv.operate.module.utils.ToastUtil;
import com.xstech.library.app.AppManager;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeoutException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

public abstract class BaseObserverCenter<T> implements Observer<T> {
    protected Context mContext;
//    private LoadingDialog mLoadingDialog;
    private String labelTxt;
    private boolean isShowLoading=true;

    public BaseObserverCenter(Context cxt, String text) {
        this.mContext = cxt;
        this.labelTxt = text;
//        mLoadingDialog = new LoadingDialog(cxt);
    }

    public BaseObserverCenter(Context cxt) {
        this.mContext = cxt;
//        mLoadingDialog = new LoadingDialog(cxt);
    }

    public BaseObserverCenter(Context cxt, boolean isShowLoading) {
        this.mContext = cxt;
        this.isShowLoading = isShowLoading;
    }

    public BaseObserverCenter() {
    }

    //开始
    @Override
    public void onSubscribe(Disposable d) {
        onRequestStart();
    }


    //失败
    @Override
    public void onError(Throwable e) {
        onRequestEnd();
        try {
            if (e instanceof ApiException) {
                ApiException apiException = (ApiException) e;
                com.xingshentech.ugv.operate.module.bean2.BaseResponseCenter baseResponse = apiException.getBaseResponse();
//                if(baseResponse.isTokenValid()){
                ToastUtil.showToast(baseResponse.getError().getErrorMessage());
//                }
            } else if (e instanceof ConnectException) {
                ToastUtil.showToast("连接异常");
            } else if (e instanceof TimeoutException || e instanceof SocketTimeoutException) {
                ToastUtil.showToast("连接超时");
            } else if (e instanceof NetworkErrorException || e instanceof UnknownHostException) {
                ToastUtil.showToast("网络错误");
            } else if (e instanceof HttpException) {
                //这里最好是让后台改一下 508 token失败或者过期，response应该还是200 ;请求应该是成功的只是token不对
                //retrofit2.adapter.rxjava2.BodyObservable; 源码  拦截了httpexception
                //response.isSuccessful() ;code >= 200 && code < 300
                // if (response.isSuccessful()) {
                //        observer.onNext(response.body());
                //      } else {
                HttpException httpException = (HttpException) e;
                if (BaseResponseCenter.TOKEN_INVALID == httpException.code()) {
                    String str = httpException.response().errorBody().string();
                    ToastUtil.showToast("token失效，请重新登录");
                } else {
                    //502 bad gateway
                    ToastUtil.showToast(httpException.getMessage() + "");
                }
            } else {
                ToastUtil.showToast("失败:" + e.getMessage());
            }
//            onNext(null);  //网络错误
            onFail(e);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    public void onFail(Throwable e) {

    }


    //结束
    @Override
    public void onComplete() {
        onRequestEnd();//请求结束
    }


    protected void onRequestStart() {
//        if (mLoadingDialog != null) {
            if (labelTxt != null && !labelTxt.equals("")) {
//                mLoadingDialog.setMessage(labelTxt);
            }
//            mLoadingDialog.show();
//        }
    }

    protected void onRequestEnd() {
//        if (mLoadingDialog != null) {
//            mLoadingDialog.dismiss();
//            progressHUD = null;
//        }
    }

    private void toActivity(Intent intent) {
        Activity activity = AppManager.getAppManager().currentActivity();
        if (activity != null && intent != null) {
            activity.startActivity(intent);
        }

    }
}

